package com.kds.smarthome.controller;

import com.kds.smarthome.service.DrawEnergySVGService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.CacheControl;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/api/energy/svg")
public class EnergySVGController {
    @Autowired
    private DrawEnergySVGService svgService;

    /**
     * averageArea 平均面积能耗
     */
    @GetMapping(value = "/ruler")
    public ResponseEntity<String> generateAvgAreaEnergyRuler(
            @RequestParam double value,
            @RequestParam(defaultValue = "375") int width,
            @RequestParam(defaultValue = "100") int height
    ) {
        log.info("Generating ruler SVG with value: {}, width: {}, height: {}", value, width, height);
        try {
            String base64Svg = svgService.generateAvgAreaEnergyRuler(value, width, height);
            return ResponseEntity.ok()
                    .contentType(MediaType.TEXT_PLAIN)
                   .cacheControl(CacheControl.maxAge(3600, TimeUnit.SECONDS))
                    .body(base64Svg);
        } catch (Exception e) {
            log.error("Error generating ruler SVG", e);
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * EER 能效比
     */
    @GetMapping("/eer")
    public ResponseEntity<String> getEERRuler(@RequestParam double value, @RequestParam(defaultValue = "375") int width,
    @RequestParam(defaultValue = "100") int height) {
        log.info("Generating EER gauge SVG with value: {}", value);
        try {
            String base64Svg = svgService.generateEERRuler(value,width, height);
            return ResponseEntity.ok()
                    .contentType(MediaType.TEXT_PLAIN)
                    .cacheControl(CacheControl.maxAge(3600, TimeUnit.SECONDS))
                    .body(base64Svg);
        } catch (Exception e) {
            log.error("Error generating EER gauge SVG", e);
            return ResponseEntity.internalServerError().build();
        }
    }
}
